﻿using HartConfigTool.CommandsQueue;
using HartConfigTool.HARTCommands.CMDs;
using HartConfigTool.SerialPortUtil;
using HartConfigTool.UI;
using HartConfigTool.UI.ExpandControls;
using HartConfigTool.UI.MainUI;
using HartConfigTool.UI.MainUI.功能模块自定义UI;
using System;
using System.Windows.Forms;

namespace HartConfigTool
{
    public partial class MainForm : Form
    {
        public static SerialPortsManager serialPortsManager;
        private static CommandDebugForm CmdDebugWin = null;
        private static InstruInformation panelInstruInfo = null;
        private static InstruInfomation_AddrSetting panelInstruInfoAddrSetting = null;

        public MainForm()
        {
            InitializeComponent();

            funcControls2.switchPanelEventHandler += SwitchPanelEventHandler;
            funcControls2.transInfoEventHandler += ExeInstrumentInfoEventHandler;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            serialPortsManager = new SerialPortsManager();
            this.cboxComX.Items.AddRange(serialPortsManager.GetSerialPortNames());
            this.cboxComX.SelectedIndex = 0;

            UpdateProgressBarSize();

            CmdDebugWin = new CommandDebugForm();

            serialPortsManager.SetShowDebugMsgControl(CmdDebugWin.txtBoxOutputInfo);

            // Instrument information
            panelInstruInfo = new InstruInformation();
            panelInstruInfo.Dock = DockStyle.Fill;

            panelInstruInfoAddrSetting = new InstruInfomation_AddrSetting();
            panelInstruInfoAddrSetting.Dock = DockStyle.Fill;

            splitContainer1.Panel2.Controls.Add(panelInstruInfo);
            splitContainer1.Panel2.Controls.Add(panelInstruInfoAddrSetting);

            panelInstruInfo.BringToFront();
        }
        private void cmdTestToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (CmdDebugWin.IsDisposed)
            {
                CmdDebugWin = new CommandDebugForm();
                serialPortsManager.SetShowDebugMsgControl(CmdDebugWin.txtBoxOutputInfo);
            }

            if (CmdDebugWin.Visible == false)
            {
                CmdDebugWin.Show();
            }
        }
        private void btnPortOpen_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.cboxComX.Text))
                {
                    this.toolStripStatusLabSysStatus.Text = "端口不能为空!";
                    return;
                }
                else
                {
                    if (serialPortsManager.Open(this.cboxComX.Text))
                    {
                        this.toolStripStatusLabSysStatus.Text = "串口打开成功!";
                    }
                    else
                    {
                        this.toolStripStatusLabSysStatus.Text = "串口打开失败!";
                    }
                }
                UpdateProgressBarSize();
            }
            catch { }
        }
        private void btnPortClose_Click(object sender, EventArgs e)
        {
            serialPortsManager.Close();
            this.toolStripStatusLabSysStatus.Text = "串口已关闭!";
            UpdateProgressBarSize();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.contextMenuFindMeter.Show(this.btnFindMeter, new System.Drawing.Point(0, btnFindMeter.Height));
        }

        private async void 查找地址0ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FindMeter FindAdrress0 = new FindMeter(serialPortsManager);
            await FindAdrress0.ExecuteCMD();
        }

        private async void 轮询015ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PollingAddress pollingAddress = new PollingAddress(serialPortsManager);

            await pollingAddress.PollingAddressStart();
        }

        private void 单点轮询ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            //MainUICMD0 CMD0 = new MainUICMD0(MainForm.serialPortsManager);
            CMD0ReadUniqueIdentifier CMD0 = new CMD0ReadUniqueIdentifier(MainForm.serialPortsManager);
            CMD0.RegisterObserver(ref CMD0.CMD0Observers, ObserversInfo.HARTCMD0Observers["HART Rev"], new TextBoxUpdater(this.txtBoxHARTRev));
            CMD0.RegisterObserver(ref CMD0.CMD0Observers, ObserversInfo.HARTCMD0Observers["Software Rev"], new TextBoxUpdater(this.txtBoxSoftwareRev));
            CMD0.RegisterObserver(ref CMD0.CMD0Observers, ObserversInfo.HARTCMD0Observers["Hardware Rev"], new TextBoxUpdater(this.txtBoxHardwareRev));
            CMD0.RegisterObserver(ref CMD0.CMD0Observers, ObserversInfo.HARTCMD0Observers["Device ID"], new TextBoxUpdater(this.txtBoxDevID));
            CMD0.RegisterObserver(ref CMD0.CMD0Observers, ObserversInfo.HARTCMD0Observers["Manufacturer ID"], new TextBoxUpdater(this.txtBoxManuID));

            CMDsQueue CommandsQueue = new CMDsQueue(MainForm.serialPortsManager);

            CommandsQueue.EnqueueCommand(CMD0, TimeSpan.FromSeconds(1));

            CommandsQueue.Start();
        }

        private void statusStrip1_Resize(object sender, EventArgs e)
        {
            UpdateProgressBarSize();
        }
        public void UpdateProgressBarSize()
        {
            toolStripProgressBar1.Width = statusStrip1.Width - toolStripStatusLabSysStatus.Width - 5;
        }
        private void SwitchPanelEventHandler(object s, EventArgs e)
        {
            Button button = s as Button;

            if (button != null)
            {
                string tag = button.Tag as string;

                switch (tag)
                {
                    case "Instrument information":
                        panelInstruInfo.BringToFront();
                        break;
                    case "Instrument information--Transmitter information":
                        panelInstruInfo.BringToFront();
                        break;
                    case "Instrument information--Address Settings":
                        panelInstruInfoAddrSetting.BringToFront();
                        break;
                    default: break;
                }
            }
        }
        private void ExeInstrumentInfoEventHandler(object s, EventArgs e)
        {

        }
        private void MainForm_Resize(object sender, EventArgs e)
        {
            splitContainer2.SplitterDistance = (int)(this.Height * 0.6);
        }
    }
}
